package rabbitmq

import (
	"fmt"

	"github.com/streadway/amqp"
)

var conn *amqp.Connection
var channel *amqp.Channel

func initChannel() bool {
	//1、判断channel是否已经创建过
	if channel != nil {
		return true
	}
	//2、获得rabbitmq的一个连接
	conn, err := amqp.Dial(RabbitURL)
	if err != nil {
		fmt.Println("err", err)
		return false
	}
	//3、打开一个channel,用于消息的发布和接收
	channel, err = conn.Channel()
	if err != nil {
		fmt.Println("err", err)
		return false
	}
	return true
}

//Publish ....
func Publish(exchange, routingKey string, msg []byte) bool {
	//1、判断channel是否正常
	if !initChannel() {
		return false
	}
	//2、执行消息发布动作
	err := channel.Publish(
		exchange,
		routingKey,
		false,
		false,
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        msg,
		},
	)
	if err != nil {
		fmt.Println("err", err)
		return false
	}
	return false
}
